import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';
import 'package:get/get.dart';
import 'package:lighting_driver/src/base/base_page.dart';
import 'package:lighting_driver/src/moduls/home/controllers/home_controller.dart';
import 'package:lighting_driver/src/moduls/home/model/model.dart';
import 'package:lighting_driver/src/moduls/home/views/drawer.dart';

import '../../../../generated/my_localizations.dart';
import '../../../const/const.dart';
import 'select_view.dart';
import 'slider_view.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final HomeController controller = Get.find<HomeController>();
  @override
  void initState() {
    super.initState();
  }

  Widget _buildText(bool isStart, String? title, IconData? icon, Color color) {
    return Row(
      mainAxisAlignment: isStart ? MainAxisAlignment.start : MainAxisAlignment.end,
      children: [
        icon == null ? const SizedBox(width: 0) : Icon(icon),
        Text(
          title ?? '',
          style: TextStyle(fontSize: 16, color: color),
        ),
      ],
    );
  }

  Widget _buildSection(String title1, String? title2, Color color1, Color color2, IconData? icon) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
      return Container(
          padding: const EdgeInsets.only(top: 0),
          height: 20,
          width: constraints.maxWidth,
          child: Row(
            children: [
              SizedBox(width: 150, child: _buildText(true, title1, icon, color1)),
              Expanded(child: Container()),
              SizedBox(width: 150, child: _buildText(false, title2, null, color2))
            ],
          ));
    });
  }

  @override
  Widget build(BuildContext context) {
    MyLocalizations loc = MyLocalizations.of(context)!;
    controller.loc = loc;
    controller.refreshLocale();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      BluetoothManager.instance.showBluetoothState();
    });
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
      return BasePage(
        child: Scaffold(
          appBar: AppBar(
            title: const Text(''),
            centerTitle: true,
            flexibleSpace: LongPressGestureDetector(
              onLongPress: () => LogRecorder.toggle(context),
              duration: const Duration(milliseconds: 5000),
              child: Container(),
            ),
            automaticallyImplyLeading: false,
            actions: [
              SizedBox(
                width: constraints.maxWidth,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: List.generate(2, ($0) {
                    return Builder(
                      builder: (context) => IconButton(
                        icon: Icon($0 == 0 ? Icons.more_horiz : Icons.settings),
                        onPressed: () => controller.clickTitle(context, $0 == 0 ? '左边抽屉' : '右边抽屉'),
                      ),
                    );
                  }),
                ),
              ),
            ],
          ),
          body: SafeArea(
            minimum: const EdgeInsets.only(left: 20, right: 20, bottom: 20),
            child: Obx(() {
              Model model = controller.selectModel.value;
              return Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                spacing: 10,
                children: [
                  _buildSection('${loc.yellow}: ${model.yellow}', '${loc.white}: ${100 - model.yellow}', yellowColor,
                      Colors.white, null),
                  Expanded(
                    flex: 7,
                    child: SliderView(
                      value: 100 - model.yellow.toDouble(),
                      colors: const [yellowColor, Colors.white],
                      onChanged: (value) => controller.changeYellow(100 - value.toInt()),
                      onChangedEnd: (value) => controller.sendData(true),
                    ),
                  ),
                  _buildSection('${loc.light}:', '${max(model.light, 5)}%', Colors.white, Colors.white,
                      Icons.light_mode_outlined),
                  Expanded(
                    flex: 7,
                    child: SliderView(
                      // min: 5,
                      value: model.light.toDouble(),
                      colors: const [Color(0xFF2F2F2F), Colors.white],
                      onChanged: (value) => controller.changeLight(value.toInt()),
                      onChangedEnd: (value) => controller.sendData(true),
                    ),
                  ),
                  _buildSection('${loc.weather}: ', controller.weather.value, Colors.white, Colors.white, null),
                  Expanded(flex: 8, child: SelectView(row: 0, bgColor: Colors.white)),
                  _buildSection('${loc.road}: ', controller.road.value, Colors.white, Colors.white, null),
                  Expanded(flex: 8, child: SelectView(row: 1, bgColor: Colors.white)),
                  _buildSection('${loc.mode}: ', controller.mode.value, Colors.white, Colors.white, null),
                  Expanded(flex: 8, child: SelectView(row: 2, bgColor: Colors.white)),
                  Expanded(flex: 8, child: SelectView(row: 3, bgColor: yellowColor)),
                  Expanded(flex: 8, child: SelectView(row: 4, bgColor: yellowColor2)),
                ],
              );
            }),
          ),
          drawer: MyDrawer(isLeft: true),
          endDrawer: MyDrawer(isLeft: false),
        ),
      );
    });
  }
}
